//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CComboBx"
#pragma resource "*.dfm"
//---------------------------------------------------------------------------
#include "vSSupprVerMenu.h"
//---------------------------------------------------------------------------
//                          Constructeur
//---------------------------------------------------------------------------
__fastcall TSupprVerMenu::TSupprVerMenu(TComponent* Owner)
    : TForm(Owner)
{
    TZZMySqlQuery * MaRequete = NULL;
    try{
        dmWebHelp = new TdmWebHelp((TComponent*)0);
        lText->Caption = MSG_VER;

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  "select distinct(log_nom),logiciels.log_code "
                                "from logiciels left outer join versions on (versions.log_code = logiciels.log_code) "
                                               "left outer join refer on (refer.log_code = logiciels.log_code) "
                                                                    "and (refer.ver_code = versions.ver_code) "
                                "where refer.ver_code is null and ver_nom is not null "
                                "group by log_nom";
        MaRequete->Open();
        MaRequete->First();

        if(!MaRequete->Eof) {
            while(!MaRequete->Eof) {
                cbLogiciel->Items->AddObject( asRequete(0), (TObject*)nRequete(1) );
                MaRequete->Next();
            }
        }else {
            cbLogiciel->Enabled = false;

            cbVersion->Items->Add("<Aucune Version à supprimer>");
            cbVersion->ItemIndex = 0;
            cbVersion->Enabled = false;
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();


    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
__fastcall TSupprVerMenu::~TSupprVerMenu()
{
    try {
        if(dmWebHelp) {delete dmWebHelp; dmWebHelp = NULL;}
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
}
//---------------------------------------------------------------------------
void __fastcall TSupprVerMenu::cbLogicielChange(TObject *Sender)
{
    TZZMySqlQuery * MaRequete = NULL;
    try{
        cbVersion->Clear();
        int nLogiciel = (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];

        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text =  (AnsiString)"select ver_nom, versions.ver_code "+
                                            "from versions left outer join refer on (versions.ver_code=refer.ver_code) "+
                                            "where versions.log_code = "+nLogiciel+" and refer.ver_code is null "+
                                            "order by ver_nom";
        MaRequete->Open();
        MaRequete->First();

        while(!MaRequete->Eof) {
            cbVersion->Items->AddObject( asRequete(0), (TObject*)nRequete(1) );
            MaRequete->Next();
        }

        MaRequete->Sql->Clear();
        MaRequete->Close();


    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//                   Suppréssion d'une version
//---------------------------------------------------------------------------
void __fastcall TSupprVerMenu::Supprimer()
{
    TZZMySqlQuery * MaRequete = NULL;
    try{
        int nLogiciel = (int)cbLogiciel->Items->Objects[cbLogiciel->ItemIndex];
        int nVersion  = (int)cbVersion->Items->Objects[cbVersion->ItemIndex];


        MaRequete = new TZZMySqlQuery( (TComponent*)0);
        MaRequete->Database = dmWebHelp->dbWebHelp;

        MaRequete->Sql->Text = (AnsiString)"delete from versions where ver_code = "+nVersion+" and log_code ="+nLogiciel;
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Sql->Text = "Commit";
        MaRequete->ExecSql();
        MaRequete->Sql->Clear();

        MaRequete->Close();

    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
}
//---------------------------------------------------------------------------
//                  Valider la suppression
//---------------------------------------------------------------------------
void __fastcall TSupprVerMenu::bbOkClick(TObject *Sender)
{
    CHistorique * Hist = NULL;
    try{
        if(    (cbLogiciel->ItemIndex != AUCUNE_SELECTION)
            && (cbVersion->ItemIndex != AUCUNE_SELECTION)) {
            Supprimer();
            ModalResult = mrOk;


            Hist = new CHistorique();
            Hist->AjouterOperation(Histo_SUPPR_VER,
                                    cbLogiciel->Items->Strings[cbLogiciel->ItemIndex],
                                    cbVersion->Items->Strings[cbVersion->ItemIndex] );
        }else {
            ModalResult = mrNone;
        }
    }catch( Exception &e ) {
        EIC_DISPLAY_EXCEPTION(e);}
    if(Hist) {delete Hist; Hist = NULL;}
}
//---------------------------------------------------------------------------
